<?php



//投资项目情况

function invest_projects_detail_invest_projects_info_table($fund_id){
    global $user;
    $query = db_select("invest_projects_detail_invest_projects_info", 'f');
    $query->fields('f',
        array(
            'id',
            'fund_id',

            'time_pass_judgement',

            'time_of_investment',

            'target_company',

            'valuation',

            'amount_of_financing',

            'amount_of_investment',

            'percentage_of_share',

            'percentage_of_fund',

            'invest_manager',

            'invest_field',

            'is_in_tsingdao',

            'is_in_early_period',

            'sum',

            'complementary_amount',

            'is_in_main_field',

            'ipo_plan',

            'current_valuation',

            'multiplication_of_benefits',

            'recent_situation',

            'record_of_first_year',

            'record_of_second_year',

            'record_of_third_year',

            'extra_remards',

        ));
    if($fund_id > 0){
        $query->condition("f.fund_id", $fund_id, '=');
    }
    $result = $query->execute();
    $rows = array();


    $header = array(
        array('data' => "公司名称"),

        array('data' => "过投决会时间"),

        array('data' => "投资时间"),

        array('data' => "项目公司"),

        array('data' => "估值"),

        array('data' => "融资额"),

        array('data' => "投资额"),

        array('data' => "占股份比例"),

        array('data' => "占基金比例"),

        array('data' => "投资经理"),

        array('data' => "领域"),

        array('data' => "是否为青岛项目"),

        array('data' => "是否为早中期项目"),

        array('data' => "合计"),

        array('data' => "冲抵金额"),

        array('data' => "是否属于主要领域"),

        array('data' => "上市计划"),

        array('data' => "当前估值"),

        array('data' => "浮动收益倍数"),

        array('data' => "企业近况"),

        array('data' => "第一年拜访记录"),

        array('data' => "第二年拜访记录"),

        array('data' => "第三年拜访记录"),

        array('data' => "备注"),

        array('data' => '选项'),
    );
    $count = 0;
    foreach($result as $row){
        $count++;
        $fund_query = db_select("fund_basic_information","f")->fields("f",array("registered_name"))
            ->condition("f.id", $row->fund_id, "=");
        $fund_result = $fund_query->execute();
        $fund = NULL;
        foreach($fund_result as $fund_row){
            $fund = $fund_row;
        }

        $newRow = array(
            'data' => array(
                'fund_name' => $fund->registered_name,

                'time_pass_judgement' => $row->time_pass_judgement,

                'time_of_investment' => $row->time_of_investment,

                'target_company' => $row->target_company,

                'valuation' => $row->valuation,

                'amount_of_financing' => $row->amount_of_financing,

                'amount_of_investment' => $row->amount_of_investment,

                'percentage_of_share' => $row->percentage_of_share,

                'percentage_of_fund' => $row->percentage_of_fund,

                'invest_manager' => $row->invest_manager,

                'invest_field' => $row->invest_field,

                'is_in_tsingdao' => $row->is_in_tsingdao,

                'is_in_early_period' => $row->is_in_early_period,

                'sum' => $row->sum,

                'complementary_amount' => $row->complementary_amount,

                'is_in_main_field' => $row->is_in_main_field,

                'ipo_plan' => $row->ipo_plan,

                'current_valuation' => $row->current_valuation,

                'multiplication_of_benefits' => $row->multiplication_of_benefits,

                'recent_situation' => $row->recent_situation,

                'record_of_first_year' => $row->record_of_first_year,

                'record_of_second_year' => $row->record_of_second_year,

                'record_of_third_year' => $row->record_of_third_year,

                'extra_remards' => $row->extra_remards,
            )
        );
        if(!in_array("引导基金管理人员", $user->roles))
            $newRow['data']['options'] = "<div>".l("编辑","projectdetail/".$fund_id."/form/".$row->id).
                "</div><div>".l("删除","projectdetail/".$fund_id."/delete/".$row->id)."</div>";
        else
            $newRow['data']['options'] = "<div>无</div>";
        $rows[] = $newRow;
    }

    $html = "<div><h3>投资项目情况信息</h3></div>";
    $html .= theme('table',
        array(
            'header' => $header,
            'rows'=>$rows,
            'sticky' => TRUE,//Optional to indicate whether the table headers should be sticky
            'empty' => '没有信息',//Optional empty text for the table if resultset is empty
        )
    );

    if(!in_array("引导基金管理人员", $user->roles))
        $html .= "<div>".l("创建信息", "projectdetail/".$fund_id."/form/0")."</div>";

    return $html;
}

function invest_projects_detail_list_view_all(){
    drupal_set_title("项目总览");
    // content variable that will be returned for display
    #echo "<script>alert('".$fund_id."');</script>";
    $html = invest_projects_detail_invest_projects_info_table(0);$html .= "<hr/>";
    return $html;
}

function invest_projects_detail_list_view() {
    drupal_set_title('基金列表');
    $html = array("#markup" => "");
    $header = array(
        array("data" => "基金名称", "field" => "registered_name"),
        array("data" => "注册资本", "field" => "registered_capital"),
        array("data" => "成立时间", "field" => "registration_date"),
        array("data" => "联系人","field" => "contact"),
        array("data" => "经营范围","field" => "scope_of_business"),
        array("data" => "基金详情","field" => "options"),
    );
    global $user;
    $rows = array();
    if(!in_array("引导基金管理人员", $user->roles)) {
        $query = "SELECT id,fund_id,registered_name" .
            " FROM {fund_basic_information} where user_id = ?";
        $queryResult = db_query($query, array($user->uid));
    }else{
        $query = "SELECT id,fund_id,registered_name" .
            " FROM {fund_basic_information}";
        $queryResult = db_query($query);
    }
    foreach ($queryResult as $links ) {
        $inside_query = db_select("fund_basic_information","n")
        ->extend("PagerDefault")
        ->limit(10)
        ->extend("TableSort")
        ->fields("n", array(
            "id",
            "registered_name",
            "registered_capital",
            "registration_date",
            "contact",
            "scope_of_business",
        ))
        ->condition('fund_id', $links->id);
        $inside_results = $inside_query->execute();
        foreach ($inside_results as $node) {
            $rows[] = array(
                "data" => array(
                    $links->registered_name,
                    $node->registered_capital,
                    $node->registration_date,
                    $node->contact,
                    $node->scope_of_business,
                    l("详情","projectdetail/".$links->id),
                ));
        }
    }
    $html["#markup"] .= theme("table",
        array(
            "header" => $header,
            "rows" => $rows,
            "sticky" => TRUE,
            "empty" => "没有记录",
        ));
    $html["#markup"] .= theme("pager",
        array(
            "tags" => array()
        ));
    return $html;
}

function invest_projects_detail_fund_view($fund_id) {
    drupal_set_title("基金列表");
    // content variable that will be returned for display
    #echo "<script>alert('".$fund_id."');</script>";
    $html = invest_projects_detail_invest_projects_info_table($fund_id);$html .= "<hr/>";
    return $html;
}




//投资项目情况

function invest_projects_detail_invest_projects_info_form($form, &$form_state, $fund_id, $id){
    $action = 1;
    if ($id > 0) $action = 2;
    $query = db_select("invest_projects_detail_invest_projects_info", 'f');
    $query = $query
        ->fields('f')
        ->condition('f.id', $id);

    $result = $query->execute();
    $rr = NULL;
    $count = 0;
    foreach ($result as $r) {
        $count++;
        $rr = $r;
    }
    $result = $rr;
    $form['tips'] = array(
        '#prefix' =>'<div id="tips">',
        '#markup' => t('<span class="form-required">*</span> 号为必填项。'),
        '#suffix' =>'</div>',
    );

    $form['time_pass_judgement'] = array(
        '#title' => t('过投决会时间'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->time_pass_judgement:"",
        //'#value' => ($count > 0)?$result->time_pass_judgement:"",
    );


    $form['time_of_investment'] = array(
        '#title' => t('投资时间'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->time_of_investment:"",
        //'#value' => ($count > 0)?$result->time_of_investment:"",
    );


    $form['target_company'] = array(
        '#title' => t('项目公司'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->target_company:"",
        //'#value' => ($count > 0)?$result->target_company:"",
    );


    $form['valuation'] = array(
        '#title' => t('估值'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->valuation:"",
        //'#value' => ($count > 0)?$result->valuation:"",
    );


    $form['amount_of_financing'] = array(
        '#title' => t('融资额'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->amount_of_financing:"",
        //'#value' => ($count > 0)?$result->amount_of_financing:"",
    );


    $form['amount_of_investment'] = array(
        '#title' => t('投资额'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->amount_of_investment:"",
        //'#value' => ($count > 0)?$result->amount_of_investment:"",
    );


    $form['percentage_of_share'] = array(
        '#title' => t('占股份比例'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->percentage_of_share:"",
        //'#value' => ($count > 0)?$result->percentage_of_share:"",
    );


    $form['percentage_of_fund'] = array(
        '#title' => t('占基金比例'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->percentage_of_fund:"",
        //'#value' => ($count > 0)?$result->percentage_of_fund:"",
    );


    $form['invest_manager'] = array(
        '#title' => t('投资经理'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->invest_manager:"",
        //'#value' => ($count > 0)?$result->invest_manager:"",
    );


    $form['invest_field'] = array(
        '#title' => t('领域'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->invest_field:"",
        //'#value' => ($count > 0)?$result->invest_field:"",
    );


    $form['is_in_tsingdao'] = array(
        '#title' => t('是否为青岛项目'),


        '#type' => 'checkbox',



        '#required' => FALSE,

        '#markup' => ($count > 0)?$result->is_in_tsingdao:"",
        //'#value' => ($count > 0)?$result->is_in_tsingdao:"",
    );


    $form['is_in_early_period'] = array(
        '#title' => t('是否为早中期项目'),


        '#type' => 'checkbox',



        '#required' => FALSE,

        '#markup' => ($count > 0)?$result->is_in_early_period:"",
        //'#value' => ($count > 0)?$result->is_in_early_period:"",
    );


    $form['sum'] = array(
        '#title' => t('合计'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->sum:"",
        //'#value' => ($count > 0)?$result->sum:"",
    );


    $form['complementary_amount'] = array(
        '#title' => t('冲抵金额'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->complementary_amount:"",
        //'#value' => ($count > 0)?$result->complementary_amount:"",
    );


    $form['is_in_main_field'] = array(
        '#title' => t('是否属于主要领域'),


        '#type' => 'checkbox',



        '#required' => FALSE,

        '#markup' => ($count > 0)?$result->is_in_main_field:"",
        //'#value' => ($count > 0)?$result->is_in_main_field:"",
    );


    $form['ipo_plan'] = array(
        '#title' => t('上市计划'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->ipo_plan:"",
        //'#value' => ($count > 0)?$result->ipo_plan:"",
    );


    $form['current_valuation'] = array(
        '#title' => t('当前估值'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->current_valuation:"",
        //'#value' => ($count > 0)?$result->current_valuation:"",
    );


    $form['multiplication_of_benefits'] = array(
        '#title' => t('浮动收益倍数'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->multiplication_of_benefits:"",
        //'#value' => ($count > 0)?$result->multiplication_of_benefits:"",
    );


    $form['recent_situation'] = array(
        '#title' => t('企业近况'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->recent_situation:"",
        //'#value' => ($count > 0)?$result->recent_situation:"",
    );


    $form['record_of_first_year'] = array(
        '#title' => t('第一年拜访记录'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->record_of_first_year:"",
        //'#value' => ($count > 0)?$result->record_of_first_year:"",
    );


    $form['record_of_second_year'] = array(
        '#title' => t('第二年拜访记录'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->record_of_second_year:"",
        //'#value' => ($count > 0)?$result->record_of_second_year:"",
    );


    $form['record_of_third_year'] = array(
        '#title' => t('第三年拜访记录'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->record_of_third_year:"",
        //'#value' => ($count > 0)?$result->record_of_third_year:"",
    );


    $form['extra_remards'] = array(
        '#title' => t('备注'),


        '#type' => 'textfield',



        '#required' => TRUE,

        '#markup' => ($count > 0)?$result->extra_remards:"",
        //'#value' => ($count > 0)?$result->extra_remards:"",
    );


    $form["fund_id"] = array(
        '#type' => 'hidden',
        '#value' => $fund_id,
    );
    $form["id"] = array(
        '#type' => 'hidden',
        '#value' => $id,
    );
    if($count > 0){
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 2,
        );
    }else{
        $form['action'] = array(
            '#type' => 'hidden',
            '#value' => 1,
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => '提交'
    );
    return $form;
}

function invest_projects_detail_invest_projects_info_form_view($fund_id, $id){
    drupal_set_title('投资项目情况');

    $render_array = array(
        '#markup' => '',
    );

    $form = drupal_get_form('invest_projects_detail_invest_projects_info_form', $fund_id, $id);
    $render_array['#markup'] .= drupal_render($form);

    return $render_array;
}


function invest_projects_detail_invest_projects_info_form_submit($form, &$form_state){
    global $user;
    $values = $form_state["values"];
    $values["uid"] = $user->uid;

    if ($values['action'] == 1)
        $last_id = db_insert('invest_projects_detail_invest_projects_info')
            ->fields(array(
                'fund_id' => $values['fund_id'],

                'time_pass_judgement' => $values['time_pass_judgement'],

                'time_of_investment' => $values['time_of_investment'],

                'target_company' => $values['target_company'],

                'valuation' => $values['valuation'],

                'amount_of_financing' => $values['amount_of_financing'],

                'amount_of_investment' => $values['amount_of_investment'],

                'percentage_of_share' => $values['percentage_of_share'],

                'percentage_of_fund' => $values['percentage_of_fund'],

                'invest_manager' => $values['invest_manager'],

                'invest_field' => $values['invest_field'],

                'is_in_tsingdao' => $values['is_in_tsingdao'],

                'is_in_early_period' => $values['is_in_early_period'],

                'sum' => $values['sum'],

                'complementary_amount' => $values['complementary_amount'],

                'is_in_main_field' => $values['is_in_main_field'],

                'ipo_plan' => $values['ipo_plan'],

                'current_valuation' => $values['current_valuation'],

                'multiplication_of_benefits' => $values['multiplication_of_benefits'],

                'recent_situation' => $values['recent_situation'],

                'record_of_first_year' => $values['record_of_first_year'],

                'record_of_second_year' => $values['record_of_second_year'],

                'record_of_third_year' => $values['record_of_third_year'],

                'extra_remards' => $values['extra_remards'],

            ))->execute();
    else if ($values['action'] == 2)
        $last_id = db_update('invest_projects_detail_invest_projects_info')
            ->fields(array(

                'time_pass_judgement' => $values['time_pass_judgement'],

                'time_of_investment' => $values['time_of_investment'],

                'target_company' => $values['target_company'],

                'valuation' => $values['valuation'],

                'amount_of_financing' => $values['amount_of_financing'],

                'amount_of_investment' => $values['amount_of_investment'],

                'percentage_of_share' => $values['percentage_of_share'],

                'percentage_of_fund' => $values['percentage_of_fund'],

                'invest_manager' => $values['invest_manager'],

                'invest_field' => $values['invest_field'],

                'is_in_tsingdao' => $values['is_in_tsingdao'],

                'is_in_early_period' => $values['is_in_early_period'],

                'sum' => $values['sum'],

                'complementary_amount' => $values['complementary_amount'],

                'is_in_main_field' => $values['is_in_main_field'],

                'ipo_plan' => $values['ipo_plan'],

                'current_valuation' => $values['current_valuation'],

                'multiplication_of_benefits' => $values['multiplication_of_benefits'],

                'recent_situation' => $values['recent_situation'],

                'record_of_first_year' => $values['record_of_first_year'],

                'record_of_second_year' => $values['record_of_second_year'],

                'record_of_third_year' => $values['record_of_third_year'],

                'extra_remards' => $values['extra_remards'],

            ))->condition('fund_id', $values['fund_id'], '=')->execute();
    else $last_id = NULL;
    if ($last_id!=NULL) {
        $form_state['redirect'] = 'projectdetail/'.$values['fund_id'];
    } else {
        // 数据写入失败了
        drupal_set_message('抱歉，遇到问题，提交失败了', 'error');
    }
}


function invest_projects_detail_delete_form_view($fund_id, $id) {
    drupal_set_title("基金列表");

    db_delete('invest_projects_detail_invest_projects_info')
        ->condition('id' ,$id)
        ->execute();
    $form_state['redirect'] = 'projectdetail/'.$id;

    // content variable that will be returned for display
    #echo "<script>alert('".$fund_id."');</script>";
    $html = invest_projects_detail_invest_projects_info_table($fund_id);$html .= "<hr/>";
    return $html;
}

?>